// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import Layout from '@/components/Layout/Layout.vue'
import { getToken } from '@/utils/auth'


const Job = ()=>import('@/views/Job.vue')
const School = ()=>import('@/views/School.vue')
const ViewRecord = ()=>import('@/views/ViewRecord.vue')
const ApplyRecord = ()=>import('@/views/ApplyRecord.vue')
const Position = ()=>import('@/views/Position.vue')


const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: 'home',
    meta: { requiresAuth: true },
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          title:'首页'
        },
        component: () => import('@/views/Home/HomeView.vue')
      },
      {
        path: '/job-seeker',
        name: 'user',
        meta: {
          title:'求职者管理'
        },
        component: () => import('@/views/User/UserView.vue')
      },
      {
        path: '/recruiter',
        name: 'recruiter',
        meta: {
          title:'招聘者管理'
        },
        component: () => import('@/views/Recruiter/Recruiter.vue')
      },
      {
        path:'/resume',
        name: 'resume',
        meta: {
          title:'简历管理'
        },
        component: () => import('@/views/Resume/ResumeView.vue')
      },
      {
        path:'/branch',
        name: 'company',
        meta: {
          title:'分行管理'
        },
        component: () => import('@/views/Company/CompanyView.vue')
      },
      { path: '/job-seeker', name: 'JobSeeker', component: () => import('@/views/JobSeeker.vue') },
      { path: '/industry', name: 'Industry', component: () => import('@/views/Industry.vue') },
      { path: '/carousel', name: 'carousel', component: () => import('@/views/carousel.vue') },
      { path: '/service', name: 'service', component: () => import('@/views/service.vue') },
      { path: '/help', name: 'help', component: () => import('@/views/help.vue') },
      { path: '/zone', name: 'zone', component: () => import('@/views/zone.vue') },
      { path: '/system/notice', name: 'notice', component: () => import('@/views/notice.vue') },
      { path: '/system/about', name: 'about', component: () => import('@/views/about.vue') },
      { path: '/system/agreement', name: 'agreement', component: () => import('@/views/agreement.vue') },
      { path: '/system/privacy', name: 'privacy', component: () => import('@/views/privacy.vue') },

      {
        path: '/job',
        component: Job
      },
      {
        path: '/school',
        component: School
      },
      {
        path: '/viewRecord',
        component: ViewRecord
      },
      {
        path: '/applyRecord',
        component: ApplyRecord
      },
      {
        path: '/position',
        component: Position
      },
    ]
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 确定是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查token是否存在
    if (!getToken()) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next() // 确保一定要调用 next()
  }
})

export default router